<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>案例01-图书购物车</title>
    <style>
        table {
            border: 1px solid #666;
            /* border-collapse: collapse; */
            border-spacing: 0px;
            width: 600px;
            text-align: center;
        }

        table th {
            border: 1px solid #666;
            height: 40px;
            line-height: 40px;
            background: #eee;
        }

        table td {
            border: 1px solid #666;
            line-height: 40px;
        }

        table td:first-child {
            min-width: 30px
        }
    </style>
</head>

<body>
    <!-- 

        图书购物车:
        要求：  图书数量增加，减少
                购物车价格随着图书数量的增加减少而改变
                图书移除
                当所有图书移除后不显示表格，显示购物车为空
        
     -->
    <div id="books">
        <table v-if="totalNum>=1">
            <tr>
                <th>ID</th>
                <th>书籍名称</th>
                <th>出版日期</th>
                <th>价格</th>
                <th>购买数量</th>
                <th>操作</th>
            </tr>
            <tr v-for="(v,index) in books" v-if="v.num>=1">
                <td>{{v.id}}</td>
                <td>{{v.name}}</td>
                <td>{{v.time}}</td>
                <!-- 价格处理的方式 -->
                <!-- <td>￥{{v.price.toFixed(2)}}</td> -->
                <!-- <td>{{getFinalPrice(v.price)}}</td>-->
                <!-- 用过滤器渲染更合适 -->
                <td>{{v.price | showPrice}}</td>
                <td>
                    <button @click="decrement(index)">-</button>
                    {{v.num}}
                    <button @click="increment(index)">+</button>
                </td>
                <td><button @click="delClick(index)">移除</button></td>
            </tr>
            <tr>
                <td colspan="6" style="text-align: left;padding-left: 10px;">总价格：{{totalPrice()}}；总数量：{{totalNum}}</td>
            </tr>
        </table>
        <h2 v-else>购物车为空</h2>
    </div>
    <script src="./js/vue.min.js"></script>
    <script>
        const books = new Vue({
            el: '#books',
            data: {
                books: [{
                        id: 1,
                        name: '算法导论',
                        time: '2006-9',
                        price: 85.00,
                        num: 1
                    },
                    {
                        id: 2,
                        name: 'UNIX编程艺术',
                        time: '2006-2',
                        price: 128.00,
                        num: 1
                    },
                    {
                        id: 3,
                        name: '编程珠玑',
                        time: '2008-10',
                        price: 99.00,
                        num: 1
                    },
                    {
                        id: 4,
                        name: '代码大全',
                        time: '2007-7',
                        price: 139.00,
                        num: 1
                    }
                ]
            },
            methods: {
                // 方法
                // 价格渲染
                getFinalPrice(price) {
                    return '￥' + price.toFixed(2);
                },
                // 图书数量增加减少
                increment(index) {
                    // console.log(index);
                    // console.log(this.books[index].num++);
                    return this.books[index].num++;
                },
                decrement(index) {
                    return this.books[index].num--;
                },
                delClick(index) {
                    return this.books[index].num = 0;
                },
                totalPrice() {
                    // 总价格
                    let totalPrice = 0;
                    for (let i = 0; i < this.books.length; i++) {
                        // console.log(typeof(this.books[i].num));
                        // console.log(typeof(this.books[i].price));
                        // console.log(this.books[i].num * this.books[i].price);
                        totalPrice += this.books[i].num * this.books[i].price;
                        // console.log(totalPrice);
                    }
                    // console.log(typeof(totalPrice));
                    // console.log(totalPrice);
                    // return totalPrice;
                    return '￥' + totalPrice.toFixed(2);

                }
            },
            computed: {
                // 计算属性
                totalNum() {
                    let totalNum = 0;
                    for (let i = 0; i < this.books.length; i++) {
                        totalNum += this.books[i].num;
                    }
                    console.log(totalNum);

                    return totalNum;
                },

            },
            filters: {
                // 过滤器
                showPrice(price) {
                    return '￥' + price.toFixed(2);
                }
            }
        })
    </script>
</body>

</html>